package com.wondersgroup.framework.sample.component.demoform.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.wondersgroup.framework.core.exception.BusinessException;
import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.component.demoform.vo.CodeInfoVO;
import com.wondersgroup.framework.sample.component.demoform.vo.FormVO;
import com.wondersgroup.framework.sample.component.demoform.vo.KeyValue;
import com.wondersgroup.framework.sample.component.demoform.vo.LinkageVO;
import com.wondersgroup.framework.sample.component.demoform.vs.DemoVS;
import com.wondersgroup.wssip.commons.dao.CommonJdbcDaoUtils;
import com.wondersgroup.wssip.util.RequestUtils;
import com.wondersgroup.wssip.util.StringTools;

public class FormAction extends BaseAjaxAction {

	private FormVO formVO = new FormVO();
	private DemoVS demoVS;

	public FormVO getFormVO() {
		return formVO;
	}

	public void setFormVO(FormVO formVO) {
		this.formVO = formVO;
	}

	public DemoVS getDemoVS() {
		return demoVS;
	}

	public void setDemoVS(DemoVS demoVS) {
		this.demoVS = demoVS;
	}

	@Override
	public ValueObject getValueObject() {
		return this.formVO;
	}

	@Override
	protected String operate() {
		String result = prefix + suffex;
		return result;
	}

	public String loadData() {
		formVO.setId("110101198001010010");
		formVO.setName("张三");
		formVO.setSex("1");
		String result = prefix + VOUtils.getJsonData(formVO) + suffex;
		createJSonData(result);
		return AJAX;
	}

	public String normalRequset() {
		formVO.setRetMsg("服务器已接收你的请求！");
		String result = prefix + VOUtils.getJsonData(formVO) + suffex;
		createJSonData(result);
		return AJAX;
	}

	public String loadCombobox() {
		String aaa147 = formVO.getAaa147(); // 行政级别
		String aaa148 = formVO.getAaa148(); // 上级行政代码
		FormVO formVo;
		List<FormVO> list = new ArrayList<FormVO>();

		String sql = "select aab301,aaa146 from aa26 where 1=1";
		if (StringTools.hasText(aaa147)) {
			sql += " and aaa147=" + aaa147;
		}

		if (StringTools.hasText(aaa148)) {
			sql += " and aaa148=" + aaa148;
		}

		sql += " order by aae005";
		List<KeyValue> kv = CommonJdbcDaoUtils.query(sql, KeyValue.class);

		String json = VOUtils.getJsonDataFromCollection(kv);
		createJSonData(json);
		return AJAX;
	}

	public String loadRadioBoxData() {
		String data1 = "{\"name\":\"男\",\"value\":\"1\",\"checked\":true,\"disabled\":true}";
		String data2 = "{\"name\":\"女\",\"value\":\"2\",\"checked\":false,\"disabled\":true}";
		String json = "[" + data1 + "," + data2 + "]";
		createJSonData(json);
		return AJAX;
	}

	public String loadCheckBoxData() {
		String data1 = "{\"text\":\"游泳\",\"code\":\"swim\",\"checked\":true,\"disabled\":true}";
		String data2 = "{\"text\":\"散步\",\"code\":\"walk\",\"checked\":true,\"disabled\":true}";
		String json = "[" + data1 + "," + data2 + "]";
		createJSonData(json);
		return AJAX;
	}

	public String loadCodeInfo() {
		List<CodeInfoVO> codeInfoList = this.getCodeInfoData();
		List<CodeInfoVO> retList = new ArrayList<CodeInfoVO>();
		CodeInfoVO model = null;
		String parentValue = servletRequest.getParameter("parentValue");
		if (StringTools.isEmpty(parentValue)) {
			for (Iterator<CodeInfoVO> iter = codeInfoList.iterator(); iter
					.hasNext();) {
				model = iter.next();
				if (model.getParent() == null) {
					retList.add(model);
				}
			}
		} else {
			for (Iterator<CodeInfoVO> iter = codeInfoList.iterator(); iter
					.hasNext();) {
				model = iter.next();
				if (model.getParent() != null
						&& model.getParent().getCode().equals(parentValue)) {
					retList.add(model);
				}
			}
		}

		String json = VOUtils.getJsonDataFromCollection(retList);
		createJSonData(json);
		return AJAX;
	}

	public List<CodeInfoVO> getCodeInfoData() {
		List<CodeInfoVO> codeInfoList = new ArrayList<CodeInfoVO>();
		CodeInfoVO model1 = new CodeInfoVO();
		model1.setId(1L);
		model1.setCode("beijing");
		model1.setName("北京市");
		model1.setValue("beijing");
		model1.setText("北京市");
		model1.setParent(null);
		codeInfoList.add(model1);
		CodeInfoVO model11 = new CodeInfoVO();
		model11.setId(11L);
		model11.setCode("dongcheng");
		model11.setName("东城区");
		model11.setValue("dongcheng");
		model11.setText("东城区");
		model11.setParent(model1);
		codeInfoList.add(model11);
		CodeInfoVO model12 = new CodeInfoVO();
		model12.setId(12L);
		model12.setCode("xicheng");
		model12.setName("西城区");
		model12.setValue("xicheng");
		model12.setText("西城区");
		model12.setParent(model1);
		codeInfoList.add(model12);

		CodeInfoVO model2 = new CodeInfoVO();
		model2.setId(2L);
		model2.setCode("shanghai");
		model2.setName("上海市");
		model2.setValue("shanghai");
		model2.setText("上海市");
		model2.setParent(null);
		codeInfoList.add(model2);
		CodeInfoVO model21 = new CodeInfoVO();
		model21.setId(21L);
		model21.setCode("xuhui");
		model21.setName("徐汇区");
		model21.setValue("xuhui");
		model21.setText("徐汇区");
		model21.setParent(model2);
		codeInfoList.add(model21);
		CodeInfoVO model22 = new CodeInfoVO();
		model22.setId(22L);
		model22.setCode("huangpu");
		model22.setName("黄浦区");
		model22.setValue("huangpu");
		model22.setText("黄浦区");
		model22.setParent(model2);
		codeInfoList.add(model22);

		CodeInfoVO model3 = new CodeInfoVO();
		model3.setId(3L);
		model3.setCode("440000");
		model3.setName("广东省");
		model3.setValue("440000");
		model3.setText("广东省");
		model3.setParent(null);
		codeInfoList.add(model3);
		CodeInfoVO model30 = new CodeInfoVO();
		model30.setId(3L);
		model30.setCode("guangzhou");
		model30.setName("广州市");
		model30.setValue("guangzhou");
		model30.setText("广州市");
		model30.setParent(model3);
		codeInfoList.add(model30);
		CodeInfoVO model31 = new CodeInfoVO();
		model31.setId(31L);
		model31.setCode("440300");
		model31.setName("深圳市");
		model31.setValue("440300");
		model31.setText("深圳市");
		model31.setParent(model3);
		codeInfoList.add(model31);
		CodeInfoVO model32 = new CodeInfoVO();
		model32.setId(32L);
		model32.setCode("dongguang");
		model32.setName("东莞市");
		model32.setValue("dongguang");
		model32.setText("东莞市");
		model32.setParent(model3);
		codeInfoList.add(model32);

		CodeInfoVO model311 = new CodeInfoVO();
		model311.setId(311L);
		model311.setCode("440303");
		model311.setName("罗湖区");
		model311.setValue("440303");
		model311.setText("罗湖区");
		model311.setParent(model31);
		codeInfoList.add(model311);
		CodeInfoVO model312 = new CodeInfoVO();
		model312.setId(6L);
		model312.setCode("440304");
		model312.setName("福田区");
		model312.setValue("440304");
		model312.setText("福田区");
		model312.setParent(model31);
		codeInfoList.add(model312);

		return codeInfoList;
	}

	public String loadFormWithLinkage() {
		LinkageVO vo = new LinkageVO();
		vo.setProvince("440000");
		vo.setCity("440300");
		vo.setDistrict("440304");
		String result = prefix + VOUtils.getJsonData(vo) + suffex;
		createJSonData(result);
		return AJAX;
	}

	public String getCascadeData() {
		LinkageVO vo = new LinkageVO();
		vo.setProvince("shanghai");
		vo.setCity("huangpu");
		String result = VOUtils.getJsonData(vo);
		createJSonData(result);
		return AJAX;
	}

	public String loadFormWithLinkage2() {
		LinkageVO vo = new LinkageVO();
		vo.setCity("huangpu");
		String result = prefix + VOUtils.getJsonData(vo) + suffex;
		createJSonData(result);
		return AJAX;
	}

	public String loadFormWithListBox() {
		String result = prefix
				+ "{\"multiselect1\":\"440303,440305\",\"multiselect2\":\"440000\"}"
				+ suffex;
		createJSonData(result);
		return AJAX;
	}

	public String submitFormWithListBox() {
		String multiselect1 = RequestUtils.getParameter(this.servletRequest,
				"multiselect1", "");
		String multiselect2 = RequestUtils.getParameter(this.servletRequest,
				"multiselect2", "");
		logger.error("multiselect1=" + multiselect1);
		logger.error("multiselect2=" + multiselect2);
		createJSonData(prefix + suffex);
		return AJAX;
	}

	public String ajaxLoadForm() {
		String aac147 = formVO.getAac147();
		String cac002 = formVO.getCac002();
		if (StringTools.hasText(aac147) && StringTools.hasText(cac002)) {
			if (!aac147.equals("111111111111111110")
					|| !cac002.equals("111111")) {
				throw new BusinessException("无匹配条件的查询结果！");
			}
		} else if (StringTools.hasText(aac147) && StringTools.isEmpty(cac002)) {
			if (!aac147.equals("111111111111111110")) {
				throw new BusinessException("无匹配条件的查询结果！");
			}
		} else if (StringTools.isEmpty(aac147) && StringTools.hasText(cac002)) {
			if (!cac002.equals("111111")) {
				throw new BusinessException("无匹配条件的查询结果！");
			}
		} else {
			throw new BusinessException("请输入查询条件！");
		}

		FormVO vo = new FormVO();
		vo.setAac147("111111111111111110");
		vo.setCac002("111111");
		vo.setAac003("赖玲玲");
		vo.setAac004("2");
		vo.setAac006("19740418");
		vo.setAae005("");
		vo.setAac067("13530853098");
		vo.setAac159("lanlinlin@wondersgroup.com");
		vo.setCac087("lanll555");
		vo.setCac088("88888888");
		vo.setCae004("1");
		vo.setCac003("440000");
		vo.setCac004("440300");
		vo.setCac005("440303");
		vo.setCac006("440303007");
		vo.setCac007("440303007011");
		String json = prefix + VOUtils.getJsonData(vo) + suffex;
		createJSonData(json);
		return AJAX;
	}

	public String ajaxSubmitForm() {
		logger.error("身份证:" + formVO.getAac147());
		logger.error("姓名:" + formVO.getAac003());
		logger.error("出生日期:" + formVO.getAac006());
		createJSonData(prefix + suffex);
		return AJAX;
	}

	public String normalSubmitForm() {
		return SUCCESS;
	}
}
